package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.*;
import com.indusfo.spc.pojo.*;
import com.indusfo.spc.service.BarCodeRules;
import com.indusfo.spc.service.OrderFlowService;
import com.indusfo.spc.service.OrderService;
import com.indusfo.spc.service.RuleService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author louk
 * @date 2019/11/23 10:33
 */
@Service
public class OrderServiceImpl implements OrderService {

    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Resource
    OrderFlowService orderFlowService;
    @Resource
    private OrderMapper orderMapper;

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Resource
    private UploadMapper uploadMapper;

    @Resource
    private BarCodeRules barCodeRules;

    @Resource
    private OrderFlowMapper orderFlowMapper;

    @Resource
    private ProStationMapper proStationMapper;

    @Resource
    private OrderStationMapper orderStationMapper;

    @Resource
    private OrderDeteMapper orderDeteMapper;

    @Resource
    private OrderBomMapper orderBomMapper;

    @Resource
    private OrderBadphenoMapper orderBadphenoMapper;

    @Resource
    MatStationMapper matStationMapper;

    @Resource
    private OrderRepairMapper orderRepairMapper;

    @Resource
    private OrderRepairFlowMapper orderRepairFlowMapper;
    @Resource
    private OrderRepairDeteMapper orderRepairDeteMapper;

    @Resource
    private RepairDetailMapper repairDetailMapper;

    @Resource
    private  OrderTechMapper orderTechMapper;

    @Resource
    private  BatchMapper batchMapper;
    @Resource
    private  RuleService ruleService;

    @Resource
    MatFlowMapper matFlowMapper;

    @Resource
    MatDeteMapper matDeteMapper;
    @Resource
    MatBomMapper matBomMapper;
    @Resource
    MatBadphenoMapper matBadphenoMapper;
    @Resource
    MatRepairMapper matRepairMapper;

    @Resource
    MatRepairFlowMapper matRepairFlowMapper;

    @Resource
    MatRepairDeteMapper matRepairDeteMapper;

    private static String LPLAN = null;

    //新增
    @Override
    public JSONObject insertPlan(Order order) {
        /*List<String> calculate = barCodeRules.calculate(6, 1);
        String ruleCode = calculate.get(0);
        order.setOrderNo(ruleCode);
        order.setDataState(1);*/
        //新增任务单默认是1 待发布的状态
        order.setExecuteStateId(1);
        //判断是否为空
        //判断是否为空
        String checkParamResult = checkParam(order);
        if(!StringUtils.isEmpty(checkParamResult)){
            return JSONObject.build(JSONObject.ERROR,checkParamResult);
        }
        int insert = orderMapper.insert(order);
        if (insert == 0) {
            throw new ModifyFailedException("数据新增失败");
        }


        return JSONObject.oK("新增成功");
    }

    //编辑
    @Override
    @Transactional
    public JSONObject updatePlan(Order order) {
        Integer orderId = order.getOrderId();

        if (orderId == null || "".equals(orderId)) {
            return  JSONObject.build(JSONObject.ERROR,"更新数据id不能为空");
        }
        /**
         * 这里查询数据库是前端不传任务单状态id做出的判断
         */
        int executeStateId = orderMapper.queryExecuteState(order);
        if(executeStateId==3 || executeStateId==5 || executeStateId==2){
            return JSONObject.oK("任务单状态为已发布/生产中/已完成,无法进行编辑");
        }
        //判断是否为空
        String checkParamResult = checkParam(order);
        if(!StringUtils.isEmpty(checkParamResult)){
            return JSONObject.build(JSONObject.ERROR,checkParamResult);
        }
        int update = orderMapper.updateByPrimaryKeySelective(order);
        if (update == 0) {
            return  JSONObject.build(JSONObject.ERROR,"数据更新失败");
        }
        return JSONObject.oK("更新成功");
    }

    //查询
    @Override
    public JSONObject queryPlan(Order order) {

        List<Order> listPlan = new ArrayList<>();
        try {
            Integer pagesize = order.getPagesize();
            Integer pageindex = order.getPageindex();
            if (pagesize != null && pageindex != null) {
                order.setIncept(pagesize * (pageindex - 1));
            }
            Integer orderId = order.getOrderId();
            //如果有id,则查询单个产线信息
            if (orderId != null && !"".equals(orderId)) {
                Order plan1 = orderMapper.queryOrder(orderId);
                listPlan.add(plan1);
            } else {  //查询多个sop管理信息
                listPlan = orderMapper.queryOrderList(order);
            }
            if (listPlan.isEmpty()) {
                return JSONObject.oK("没有查询到相关数据", listPlan, 0);
            }

            //查询分页总记录数
            int count = orderMapper.countOrderList(order);
            return JSONObject.oK("查询成功", listPlan, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    //删除
    @Override
    public JSONObject deletePlan(Integer[] ids, Integer lDataState, Integer[] lExecuteStateIds) {

        try {
            if (lDataState == null) {
                return  JSONObject.build(JSONObject.ERROR,"数据状态不能为空");
            } else if (lDataState != 1 && lDataState != 2 && lDataState != 3) {
                return  JSONObject.build(JSONObject.ERROR,"数据状态错误");
            }
            if (ids == null) {
                return  JSONObject.build(JSONObject.ERROR,"请选择要删除的数据");
            }
            if (lExecuteStateIds == null || lExecuteStateIds.length == 0) {
                return  JSONObject.build(JSONObject.ERROR,"当前状态未传递");
            }
            for (Integer lExecuteStateId : lExecuteStateIds) {
                if (lExecuteStateId != 1) {
                    return  JSONObject.build(JSONObject.ERROR,"所选任务单中包含状态为非待发布的任务单,无法删除");
                }
            }
            // 执行存储过程
            int row = orderMapper.updateDataState(ids, lDataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (lDataState) {
                    case 1:
                        return  JSONObject.build(JSONObject.ERROR,"启用失败");
                    case 2:
                        return  JSONObject.build(JSONObject.ERROR,"删除失败");
                    case 3:
                        return  JSONObject.build(JSONObject.ERROR,"停用失败");
                    default:
                }
            } else {
                switch (lDataState) {
                    case 1:
                        msg = "启用成功";
                        break;
                    case 2:
                        msg = "删除成功";
                        break;
                    case 3:
                        msg = "停用成功";
                        break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    //状态变更
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject lExecuteStateChange(Integer[] ids, Integer[] lExecuteStateIds, Integer lExecuteState) {

        try {
            if (lExecuteState == null) {
                return JSONObject.build(JSONObject.ERROR,"发布状态不能为空");
            } else if (lExecuteState != 1 && lExecuteState != 2 && lExecuteState != 3 && lExecuteState != 4 && lExecuteState != 5  && lExecuteState != 6) {
                return JSONObject.build(JSONObject.ERROR,"数据状态错误");
            }
            if (ids == null) {
                return JSONObject.build(JSONObject.ERROR,"请选择要更改的数据");
            }
            if (lExecuteStateIds == null || lExecuteStateIds.length == 0) {
                return JSONObject.build(JSONObject.ERROR,"任务单状态未传递");
            }
            //当当前任务状态中包含已完工的不能进行状态修改
            for (Integer lExecuteStateId : lExecuteStateIds) {
                if(lExecuteStateId==5){
                    return JSONObject.build(JSONObject.ERROR,"任务状态为已完工时不能进行状态修改");
                }
            }
            //当要进行发布时,判断当前状态是否为已发布(2),生产中(3),暂停(4),已完工(5),如果是则不能进行发布
            if(lExecuteState==2){
                for (Integer lExecuteStateId : lExecuteStateIds) {
                    if(lExecuteStateId!=1){
                        return JSONObject.build(JSONObject.ERROR,"所选择的任务单中有状态为已发布或生产中、暂停、已完工的任务单无法进行发布");
                    }
                }
                int updateLastExecuteStateIssue = orderMapper.updateExecuteStateIssue(ids, lExecuteState);
                if(updateLastExecuteStateIssue>0){
                    orderLeadOrderFlow(ids);
                    return JSONObject.oK("发布成功");
                }
            }
            //当要进行暂停时,只有是生产中才可以暂停,并且需要将上次的状态进行存储
            if(lExecuteState==4){
                for (Integer lExecuteStateId : lExecuteStateIds) {
                    if(lExecuteStateId!=3){
                        return JSONObject.build(JSONObject.ERROR,"所选择的任务单状态在生产中才可以暂停");
                    }
                }
                //跟上述sql区别
                SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
                OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
                for (int i = 0; i < ids.length; i++) {
                    //更新对应的数据状态
                    mapper.updateLastExecuteState(ids[i], lExecuteStateIds[i]);
                }
                int executeStateChange = mapper.executeStateChange(ids, lExecuteState);
                if(executeStateChange!=0){
                    //sql语句提交
                    sqlSession.commit();
                    //清楚缓存
                    sqlSession.clearCache();
                    return JSONObject.oK("暂停成功");
                }else{
                    // sql语句不进行提交
                    // sqlSession.commit();
                    //清楚缓存
                    sqlSession.clearCache();
                    return JSONObject.oK("暂停失败");
                }

            }
            //当要进行恢复时,只有暂停的可以进行恢复,恢复到上次的状态
            if(lExecuteState==6){
                for (Integer lExecuteStateId : lExecuteStateIds) {
                    if(lExecuteStateId!=4){
                        return JSONObject.build(JSONObject.ERROR,"所选择的任务单状态为暂停才可以恢复");
                    }
                }
                int updateByLastExecuteState = orderMapper.updateByLastExecuteState(ids);
                if (updateByLastExecuteState > 0) {
                    return JSONObject.oK("恢复成功");
                }
            }
            //当点击完工时,生产中 和已发布的可以点击完工
            if(lExecuteState==5){
                for (Integer lExecuteStateId : lExecuteStateIds) {
                    if(lExecuteStateId!=2 && lExecuteStateId!=3){
                        return JSONObject.build(JSONObject.ERROR,"所选择的任务单状态为生产中或已发布才可以进行完工");
                    }
                }
                int executeStateChange = orderMapper.executeStateAndDateChange(ids, lExecuteState);

                if(executeStateChange>0){

                    return JSONObject.oK("成功完工");
                }

            }
            return JSONObject.build(JSONObject.ERROR,"状态修改失败");
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            //手动强制回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return JSONObject.build(403, e.getMessage());
        }
    }

    //获取版本
    @Override
    public JSONObject getVersion(Integer lProduct) {
        if (lProduct == null) {
            throw new ParamsErrorException("产品ID为空");
        }
        MatVer version = orderMapper.getVersion(lProduct);
        return JSONObject.oK("查询成功", version, 1);
    }


    private String checkParam(Order order) {
        Date endDate;
        Date startDate;
        Date actualStartDate = new Date();
        Date actualFinishDate = new Date();
        //订单类型
        Integer orderType = order.getOrderTypeId();
        //任务单编号
        String orderNo = order.getOrderNo();
        //物料id
        Integer matId = order.getMatId();
        //版本号
        Integer versionNumber = order.getMatVerId();
        //产线id
        Integer productionLineId = order.getProductlineId();
        //班次id
        Integer classes = order.getClasseId();
        //用户id
        Integer userId = order.getUserId();
        //开始时间
        String startDateTime = order.getStartDate();
        //结束时间
        String endDateTime = order.getEndDate();
        //计划数量
        Integer planNum = order.getPlanNum();
        Integer workshopId = order.getWorkshopId();

        if(StringUtils.isEmpty(orderNo)){
//            List<String> calculate = barCodeRules.calculate(6, 1);

            List<String> calculate   =ruleService.otherRule(6,1);
            String ruleCode = calculate.get(0);
            order.setOrderNo(ruleCode);
            order.setDataState(1);
        }else{
            if(orderNo.length()>50){
                return "生产任务单号超过规定长度";
            }
            int queryDistinctOrderNo = orderMapper.queryDistinctOrderNo(order);
            if (queryDistinctOrderNo>0){
                return "任务单号已存在";
            }
        }

        if (order.getRemark() != null && order.getRemark().length() > 100) {
            return "说明超过规定长度";
        }

        if (orderType == null || "".equals(orderType) || new Integer(0).equals(orderType)) {
            return "订单类型不能为空";
        }
        if (matId == null || "".equals(matId) || new Integer(0).equals(matId)) {
            return "物料id不能为空";
        }
        if (versionNumber == null || "".equals(versionNumber) || new Integer(0).equals(versionNumber) ) {
            return "版本号不能为空";
        }
        if(planNum==null || "".equals(planNum) || new Integer(0).equals(planNum) ){
            return "计划数量为空";
        }
        if(!StringUtils.isNumeric(String.valueOf(planNum))){
            return "计划数量必须为整数";
        }
        if(workshopId==null || "".equals(workshopId) || new Integer(0).equals(workshopId)){
            return "车间不能为空";
        }
        if (productionLineId == null || "".equals(productionLineId) || new Integer(0).equals(productionLineId)) {
            return "车间产线不能为空";
        }
        if (classes == null || "".equals(classes) || new Integer(0).equals(classes)) {
            return "班次不能为空";
        }
        if (userId == null || "".equals(userId) || new Integer(0).equals(userId) ) {
            return "用户id不能为空";
        }
        if (StringUtils.isBlank(startDateTime)) {
            return "计划开始时间不能为空";
        }
        if (StringUtils.isBlank(endDateTime)) {
            return "计划结束时间不能为空";
        }
        try {
            startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDateTime);

        } catch (Exception exception) {
            return "开始时间格式不正确";
        }
        try {
            endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDateTime);

        } catch (Exception exception) {
            return "结束时间格式不正确";
        }
        if (startDate.getTime() > endDate.getTime()) {
            return  "开始时间不能大于结束时间";
        }
        /*if (!StringUtils.isBlank(order.getActualStartDate())) {
            try {
                actualStartDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(order.getActualStartDate());
            } catch (Exception exception) {
                return "实际开始时间格式不正确";
            }
        }
        if (!StringUtils.isBlank(order.getActualEndDate())) {
            try {
                actualFinishDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(order.getActualEndDate());
            } catch (Exception exception) {
                return "实际结束时间格式不正确";
            }
        }
        if (!StringUtils.isBlank(order.getActualStartDate()) && !StringUtils.isBlank(order.getActualEndDate())) {
            if (actualStartDate.getTime() > actualFinishDate.getTime()) {
               return "实际开始时间不能大于实际结束时间";
            }
        }*/
        if( order.getActualStartDate()!=null && order.getActualEndDate()!=null ) {
            if( order.getActualStartDate().compareTo(order.getActualEndDate())>0 ) {
                return "实际开始时间不能大于实际结束时间";
            }
        }

        return null;
    }

    @Override
    public String checkExcelContent(Order order) {
        if (LPLAN == null) {
            getLPlan();
        }
        order.setOrderNo("");
        Date overDate;
        Date startDate;
        //订单类型名称
        String orderTypeName = order.getOrderTypeName();
        //版本号
        String vcVersion = order.getMatVerName();
        //产线名称
        String vcProductionLineName = order.getProductlineName();
        //班次名称
        String vcClasses = order.getClasseName();
        //计划数量
        Integer lPlanNumber = order.getPlanNum();
        //物料名称/产品名称
        String vcProjectName = order.getMatName();
        //制单人
        String vcUserName = order.getUserName();
        //开始时间
        String startTime = order.getStartDate();
        //结束时间
        String overTime = order.getEndDate();
        if (StringUtils.isBlank(orderTypeName) && StringUtils.isBlank(vcVersion) && StringUtils.isBlank(vcProductionLineName) && StringUtils.isBlank(vcClasses) && StringUtils.isBlank(lPlanNumber.toString()) &&
                StringUtils.isBlank(vcProjectName) && StringUtils.isBlank(vcUserName) && StringUtils.isBlank(startTime) && StringUtils.isBlank(overTime)) {
            return "该行数据为空(原因:清空行内容时未清空彻底/未输入)";
        }
        if (order.getRemark() != null && order.getRemark().length() > 100) {
            return "说明超过规定长度";
        }
        if (orderTypeName == null) {
            return "订单类型名称为空";
        }
        Map<String, Datadict> orderTypeMap = uploadMapper.queryType(101012);
        if (!orderTypeMap.containsKey(orderTypeName)) {
            return "订单类型名称填写不规范/错误";
        }
        Datadict orderTypeTdatadict = orderTypeMap.get(orderTypeName);
        order.setOrderTypeId(orderTypeTdatadict.getItemId());
        if (vcVersion == null) {
            return "版本号为空";
        }
        if (vcProductionLineName == null) {
            return "产线名称为空";
        }
        if (vcClasses == null) {
            return "班次名称为空";
        }
        if (lPlanNumber == null) {
            return "计划数量为空";
        }
        if (overTime == null) {
            return "结束时间为空";
        }
        if (startTime == null) {
            return "开始时间为空";
        }
        if (vcProjectName == null) {
            return "产品名称为空";
        }
        if (vcUserName == null) {
            return "制单人为空";
        }

        try {
            startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
        } catch (Exception exception) {
            return "开始时间格式不正确";
        }
        try {
            overDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(overTime);

        } catch (Exception exception) {
            return "结束时间格式不正确";
        }
        if (startDate.getTime() > overDate.getTime()) {
            return "开始时间不能大于结束时间";
        }

        return null;
    }

    private void getLPlan() {
//        List<String> calculate = barCodeRules.calculate(6, 1);
        List<String> calculate   =ruleService.otherRule(6,1);
        LPLAN = calculate.get(0);
    }
	@Override
    public JSONObject copyOrderFlow(Order order){
        try {
            //判空
            if (order == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (order.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=order.getBatchId();
            if (order.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=order.getOrderTechId();

            JSONObject jsonObject=new JSONObject();



            if(order.getBatchId().equals(0)){
                //任务单保存

                //复制新流程
                //查询旧版本
                int row= orderMapper.queryOrderTechCount(oldOrderTechId);
                if(row!=0){
                    Integer newOrderTechId=copyTech(oldOrderTechId,1);

                //复制流程完成
                //获取前端流程
                List<OrderFlow> orderFlowList9 = order.getOrderFlowList();
                //查询新流程
                OrderFlow orderFlow8=new OrderFlow();
                orderFlow8.setOrderTechId(newOrderTechId);
                List<OrderFlow> orderFlowList8 = orderFlowMapper.queryOrderFlow(orderFlow8);
                //查询旧流程
                OrderFlow orderFlow7=new OrderFlow();
                orderFlow7.setOrderTechId(oldOrderTechId);
                List<OrderFlow> orderFlowList7 = orderFlowMapper.queryOrderFlow(orderFlow7);
                for (OrderFlow orderFlow6:orderFlowList9 ) {
                    if(!orderFlow6.getOrderFlowId().equals(0)){
                        for (int i = 0; i <orderFlowList7.size() ; i++) {
                            if(orderFlowList7.get(i).getOrderFlowId().equals(orderFlow6.getOrderFlowId())){
                                orderFlow6.setOrderFlowId(orderFlowList8.get(i).getOrderFlowId());
                            }
                        }
                    }
                }
                //复制新对象

                Order   newOrder=new Order();
                BeanUtils.copyProperties(order,newOrder);
                newOrder.setOrderTechId(newOrderTechId);
                newOrder.setOrderFlowList(null);
                newOrder.setOrderRepairList(null);
                newOrder.setOrderFlowList(orderFlowList9);
                //修改新流程
                jsonObject=saveOrderFlow(newOrder);
                }else{
                    jsonObject=saveOrderFlow(order);
                }


//









            }else{
                //批次保存
                OrderTech orderTech10=new OrderTech();
                orderTech10.setOrderTechId(order.getOrderTechId());
                List<OrderTech> orderTeches10 = orderTechMapper.queryOrderTech(orderTech10);
                if(orderTeches10.size()!=0){
                    orderTech10=orderTeches10.get(0);
                }
                System.out.println(orderTech10.getOrderTechId()+"-"+orderTech10.getSourceTypeId());
                if(orderTech10.getSourceTypeId().equals(2)){
                    jsonObject=saveOrderFlow(order);

                }else{
                    //任务单保存

                    //复制新流程
                    //查询旧版本

                    Integer newOrderTechId=copyTech(oldOrderTechId,2);
                    //复制流程完成
                    //获取前端流程
                    List<OrderFlow> orderFlowList9 = order.getOrderFlowList();
                    //查询新流程
                    OrderFlow orderFlow8=new OrderFlow();
                    orderFlow8.setOrderTechId(newOrderTechId);
                    List<OrderFlow> orderFlowList8 = orderFlowMapper.queryOrderFlow(orderFlow8);
                    //查询旧流程
                    OrderFlow orderFlow7=new OrderFlow();
                    orderFlow7.setOrderTechId(oldOrderTechId);
                    List<OrderFlow> orderFlowList7 = orderFlowMapper.queryOrderFlow(orderFlow7);
                    for (OrderFlow orderFlow6:orderFlowList9 ) {
                        if(!orderFlow6.getOrderFlowId().equals(0)){
                            for (int i = 0; i <orderFlowList7.size() ; i++) {
                                if(orderFlowList7.get(i).getOrderFlowId().equals(orderFlow6.getOrderFlowId())){
                                    orderFlow6.setOrderFlowId(orderFlowList8.get(i).getOrderFlowId());
                                }
                            }
                        }
                    }
                    //复制新对象

                    Order   newOrder=new Order();
                    BeanUtils.copyProperties(order,newOrder);
                    newOrder.setOrderTechId(newOrderTechId);
                    newOrder.setOrderFlowList(null);
                    newOrder.setOrderRepairList(null);
                    newOrder.setOrderFlowList(orderFlowList9);
                    //修改新流程



                    jsonObject=saveOrderFlow(newOrder);

                    Batch batch=new Batch();
                    batch.setBatchId(order.getBatchId());
                    batch.setOrderTechId(newOrderTechId);
                    batchMapper.updateBatch(batch);



                }

                //更新批次流程的当前工序
                Batch batch=new Batch();
                batch.setBatchId(order.getBatchId());
                Integer nowFlowId = orderTechMapper.selectNowFlowId(order.getBatchId());
                if(nowFlowId==null){
                    nowFlowId=orderTechMapper.selectLastFlowId(order.getBatchId());
                }
                batch.setOrderFlowId(nowFlowId);
                batchMapper.updateBatch(batch);





            }


            return jsonObject;
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 保存工序流程
     */
    @Override
    public JSONObject saveOrderFlow(Order order) {
        try {

            if (order == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (order.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=order.getBatchId();
            if (order.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=order.getOrderTechId();

            //获取版本
//            Integer orderId = order.getOrderId();
            //获取list集合
            List<OrderFlow> orderFlowList = new ArrayList<>();
            orderFlowList = order.getOrderFlowList();
            for (OrderFlow orderFlow : orderFlowList) {
                orderFlow.setOrderTechId(oldOrderTechId);
            }


            //获取数据库数据
            OrderFlow orderFlow1 = new OrderFlow();
            orderFlow1.setOrderTechId(oldOrderTechId);

            List<OrderFlow> oldorderFlowList = orderFlowMapper.queryOrderFlow(orderFlow1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldorderFlowList != null) {
                for (OrderFlow orderFlow : oldorderFlowList
                ) {
                    oldIds.add(orderFlow.getOrderFlowId());
                }
            }
            if (orderFlowList != null) {
                for (OrderFlow orderFlow : orderFlowList
                ) {
                    newIds.add(orderFlow.getOrderFlowId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = orderFlowMapper.deleteOrderFlow(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (orderFlowList != null) {
                for (OrderFlow orderFlow : orderFlowList
                ) {
                    if (orderFlow.getDataState() == null) {
                        orderFlow.setDataState(1);
                    }
                    if (orderFlow.getSequence() == null) {
                        orderFlow.setSequence(1);
                    }

                    if (orderFlow.getOrderFlowId() != null) {
                        if (orderFlow.getOrderFlowId().equals(0)) {
                            orderFlow.setOrderFlowId(null);
                            insertrow = insertrow + orderFlowMapper.insertOrderFlow(orderFlow);
                            //带出  工位和不良
                            Integer newFlow = orderFlow.getOrderFlowId();
                            Integer proId = orderFlow.getProId();

                            //查询工序下工位
                            Pro pro = new Pro();
                            pro.setProId(proId);

                            List<ProStation> proStations = proStationMapper.queryByProId(proId);
                            //带出  工位
                            for (int i = 0; i < proStations.size(); i++) {
                                OrderStation orderStation = new OrderStation();
                                orderStation.setOrderFlowId(newFlow);
                                orderStation.setStationId(proStations.get(i).getStationId());
                                orderStation.setDataState(1);
                                orderStationMapper.insertOrderStation(orderStation);
                            }

                            //带出  工位和不良  完成

                        } else {
                            updaterow = updaterow + orderFlowMapper.updateOrderFlow(orderFlow);
                        }
                    }
                }
            }

            return JSONObject.oK(HintMessage.SAVE_SUCCESS, order,deleterow+insertrow+updaterow);


        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }
    /**
     * 复制维修流程
     */
    @Override
    public JSONObject copyOrderRepair(Order order) {
        try {
            //判空
            if (order == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (order.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=order.getBatchId();
            if (order.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=order.getOrderTechId();

            JSONObject jsonObject=new JSONObject();



            if(order.getBatchId().equals(0)){
                //任务单保存

                //查看当前版本是否被使用
                int row= orderMapper.queryOrderTechCount(oldOrderTechId);
                if(row!=0){
                    //复制版本
                Integer newOrderTechId=copyTech(oldOrderTechId,1);

                //获取前端流程
                //获取前端流程
                List<OrderRepair> orderRepairList9 = order.getOrderRepairList();
                //查询新流程
                OrderRepair orderRepair8=new OrderRepair();
                orderRepair8.setOrderTechId(newOrderTechId);
                List<OrderRepair> orderRepairList8 = orderRepairMapper.queryOrderRepair(orderRepair8);
                //查询旧流程
                OrderRepair orderRepair7=new OrderRepair();
                orderRepair7.setOrderTechId(oldOrderTechId);
                List<OrderRepair> orderRepairList7 = orderRepairMapper.queryOrderRepair(orderRepair7);
                for (OrderRepair orderRepair6:orderRepairList9 ) {
                    if(!orderRepair6.getOrderRepairId().equals(0)){
                        for (int i = 0; i <orderRepairList7.size() ; i++) {
                            if(orderRepairList7.get(i).getOrderRepairId().equals(orderRepair6.getOrderRepairId())){
                                orderRepair6.setOrderRepairId(orderRepairList8.get(i).getOrderRepairId());
                            }
                        }
                    }
                }
                //复制新对象

                Order   newOrder=new Order();
                BeanUtils.copyProperties(order,newOrder);
                newOrder.setOrderTechId(newOrderTechId);
                newOrder.setOrderRepairList(null);
                newOrder.setOrderRepairList(null);
                newOrder.setOrderRepairList(orderRepairList9);
                //修改新流程
                jsonObject=saveOrderRepair(newOrder);
                }else{
                    jsonObject=saveOrderRepair(order);
                }


//
//








            }else{
                //批次保存
                OrderTech orderTech10=new OrderTech();
                orderTech10.setOrderTechId(order.getOrderTechId());
                List<OrderTech> orderTeches10 = orderTechMapper.queryOrderTech(orderTech10);
                if(orderTeches10.size()!=0){
                    orderTech10=orderTeches10.get(0);
                }
                System.out.println(orderTech10.getOrderTechId()+"-"+orderTech10.getSourceTypeId());
                if(orderTech10.getSourceTypeId().equals(2)){
                    jsonObject=saveOrderRepair(order);

                }else{
                    //任务单保存

                    //复制版本
                    Integer newOrderTechId=copyTech(oldOrderTechId,2);

                    //复制流程完成
                    //获取前端流程
                    List<OrderRepair> orderRepairList9 = order.getOrderRepairList();
                    //查询新流程
                    OrderRepair orderRepair8=new OrderRepair();
                    orderRepair8.setOrderTechId(newOrderTechId);
                    List<OrderRepair> orderRepairList8 = orderRepairMapper.queryOrderRepair(orderRepair8);
                    //查询旧流程
                    OrderRepair orderRepair7=new OrderRepair();
                    orderRepair7.setOrderTechId(oldOrderTechId);
                    List<OrderRepair> orderRepairList7 = orderRepairMapper.queryOrderRepair(orderRepair7);
                    for (OrderRepair orderRepair6:orderRepairList9 ) {
                        if(!orderRepair6.getOrderRepairId().equals(0)){
                            for (int i = 0; i <orderRepairList7.size() ; i++) {
                                if(orderRepairList7.get(i).getOrderRepairId().equals(orderRepair6.getOrderRepairId())){
                                    orderRepair6.setOrderRepairId(orderRepairList8.get(i).getOrderRepairId());
                                }
                            }
                        }
                    }
                    //复制新对象

                    Order   newOrder=new Order();
                    BeanUtils.copyProperties(order,newOrder);
                    newOrder.setOrderTechId(newOrderTechId);
                    newOrder.setOrderRepairList(null);
                    newOrder.setOrderRepairList(null);
                    newOrder.setOrderRepairList(orderRepairList9);
                    //修改新流程



                    jsonObject=saveOrderRepair(newOrder);

                    Batch batch=new Batch();
                    batch.setBatchId(order.getBatchId());
                    batch.setOrderTechId(newOrderTechId);
                    batchMapper.updateBatch(batch);

                }
                //更新批次流程的当前工序
                Batch batch=new Batch();
                batch.setBatchId(order.getBatchId());
                Integer nowFlowId = orderTechMapper.selectNowFlowId(order.getBatchId());
                if(nowFlowId==null){
                    nowFlowId=orderTechMapper.selectLastFlowId(order.getBatchId());
                }
                batch.setOrderFlowId(nowFlowId);
                batchMapper.updateBatch(batch);





            }


            return jsonObject;
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }
/**
 * @Author 熊冰 复制流程
 * @Date 2020/1/13 13:08
 */
    @Override
    public JSONObject copyOrderTech(Integer orderTechId) {
        try{
            if(orderTechId==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer newOrderTechId=copyTech(orderTechId,1);
            Order order1=new Order();
            order1.setOrderId(orderMapper.queryOrderIdByOrderTechId(orderTechId));
            order1.setOrderTechId(newOrderTechId);
            orderMapper.updateByPrimaryKeySelective(order1);

            return JSONObject.oK(HintMessage.COPY_SUCCESS, copyTech(orderTechId,1),0);
    } catch (GlobalException e) {// 捕获自定义异常
        logger.error(e.getMessage());
        return JSONObject.build(500, e.getMessage());
    }
    }

    /**
     * 查询任务单(任务单流程)
     */
    @Override
    public JSONObject selectOrder(Order order) {
        List<Order> listPlan = null;
        try {
            Integer pagesize = order.getPagesize();
            Integer pageindex = order.getPageindex();
            if (pagesize != null && pageindex != null) {
                order.setIncept(pagesize * (pageindex - 1));
            }
            listPlan = orderMapper.selectOrder(order);
            if (listPlan.isEmpty()) {
                return JSONObject.oK("没有查询到相关数据", listPlan, 0);
            }
            //查询分页总记录数
            int count = orderMapper.countOrder(order);
            return JSONObject.oK("查询成功", listPlan, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }


    public Integer copyTech(Integer oldOrderTechId,Integer sourceTypeId ){
        //复制新流程
        //查询旧版本


        OrderTech oldOrderTech=new OrderTech();
        oldOrderTech.setOrderTechId(oldOrderTechId);
        //查询原版本
        List<OrderTech> orderTeches = orderTechMapper.queryOrderTech(oldOrderTech);
        if(orderTeches.size()!=0){
            oldOrderTech=orderTeches.get(0);
        }
        //创建新版本



        OrderTech newOrderTech=new OrderTech();
        BeanUtils.copyProperties(oldOrderTech,newOrderTech);
        newOrderTech.setOrderTechId(null);
        newOrderTech.setSourceTypeId(sourceTypeId);

        orderTechMapper.insertOrderTech(newOrderTech);
        Integer  newOrderTechId=newOrderTech.getOrderTechId();
        //新增完成

        //复制流程
        OrderFlow orderFlow1=new OrderFlow();
        orderFlow1.setOrderTechId(oldOrderTechId);
        List<OrderFlow> orderFlowList = orderFlowMapper.queryOrderFlow(orderFlow1);

        List<OrderStation> orderStationInsertList=new ArrayList<>();
        List<OrderDete> orderDeteInsertList=new ArrayList<>();
        List<OrderBom> orderBomInsertList=new ArrayList<>();
        List<OrderBadpheno> orderBadphenoInsertList=new ArrayList<>();
        List<OrderRepairFlow> orderRepairFlowInsertList=new ArrayList<>();
        List<OrderRepairDete> orderRepairDeteInsertList=new ArrayList<>();

        for (OrderFlow oldOrderFlow:orderFlowList ) {

            OrderFlow orderFlowInsert=new OrderFlow();

            Integer oldOrderFlowId=oldOrderFlow.getOrderFlowId();

            OrderFlow newOrderFlow=new OrderFlow();
            BeanUtils.copyProperties(oldOrderFlow,newOrderFlow);
            newOrderFlow.setOrderTechId(newOrderTechId);
            newOrderFlow.setOrderFlowId(null);
            //新增流程
            orderFlowMapper.insertOrderFlow(newOrderFlow);
            Integer newOrderFlowId=newOrderFlow.getOrderFlowId();

            //复制工位

            List<OrderStation> orderStations = orderStationMapper.queryOrderStationByOrderFlowId(oldOrderFlowId);
            for (OrderStation orderStation:orderStations ) {
                orderStation.setOrderFlowId(newOrderFlowId);
                orderStation.setOrderStationId(null);
                orderStationInsertList.add(orderStation);
//                orderStationMapper.insertOrderStation(orderStation);
            }

            List<OrderDete> orderDetes = orderDeteMapper.queryOrderDeteByOrderFlowId(oldOrderFlowId);
            for (OrderDete orderDete:orderDetes ) {
                orderDete.setOrderFlowId(newOrderFlowId);
                orderDete.setOrderDeteId(null);
                orderDeteInsertList.add(orderDete);
//                orderDeteMapper.insertOrderDete(orderDete);
            }

            List<OrderBom> orderBoms = orderBomMapper.queryOrderBomByOrderFlowId(oldOrderFlowId);
            for (OrderBom orderBom:orderBoms ) {
                orderBom.setOrderFlowId(newOrderFlowId);
                orderBom.setOrderBomId(null);
                orderBomInsertList.add(orderBom);
//                orderBomMapper.insertOrderBom(orderBom);
            }

            List<OrderBadpheno> orderBadphenos = orderBadphenoMapper.queryOrderBadphenoByOrderFlowId(oldOrderFlowId);
            for (OrderBadpheno orderBadpheno:orderBadphenos ) {
                orderBadpheno.setOrderFlowId(newOrderFlowId);
                orderBadpheno.setOrderBadphenoId(null);
                orderBadphenoInsertList.add(orderBadpheno);
//                orderBadphenoMapper.insertOrderBadpheno(orderBadpheno);
            }

        }

        OrderRepair orderRepair1=new OrderRepair();
        orderRepair1.setOrderTechId(oldOrderTechId);
        List<OrderRepair> orderRepairList = orderRepairMapper.queryOrderRepair(orderRepair1);

        for (OrderRepair oldOrderRepair:orderRepairList ) {
            Integer oldOrderRepairId= oldOrderRepair.getOrderRepairId();
            OrderRepair newOrderRepair=new OrderRepair();
            BeanUtils.copyProperties(oldOrderRepair,newOrderRepair);
            newOrderRepair.setOrderRepairId(null);
            newOrderRepair.setOrderTechId(newOrderTechId);
            orderRepairMapper.insertOrderRepair(newOrderRepair);
            Integer newOrderRepairId=newOrderRepair.getOrderRepairId();

            List<OrderRepairFlow> orderRepairFlows = orderRepairFlowMapper.queryOrderRepairFlowByOrderRepairId(oldOrderRepairId);
            for (OrderRepairFlow oldOrderRepairFlow:orderRepairFlows ) {

                //查询工艺流程
                OrderFlow orderFlow = new OrderFlow();
                orderFlow.setOrderTechId(newOrderTechId);
                List<OrderFlow> orderFlowList1 = orderFlowMapper.queryOrderFlow(orderFlow);
                for (OrderFlow orderFlow2:orderFlowList1) {
                    if(oldOrderRepairFlow.getProId()!=null&&orderFlow2.getProId()!=null){
                        if(oldOrderRepairFlow.getProId().equals(orderFlow2.getProId())&&oldOrderRepairFlow.getSerialNum().equals(orderFlow2.getSerialNum())){
                            OrderRepairFlow newOrderRepairFlow=new OrderRepairFlow();
                            newOrderRepairFlow.setDataState(1);
                            newOrderRepairFlow.setOrderRepairFlowId(null);
                            newOrderRepairFlow.setOrderRepairId(newOrderRepairId);
                            newOrderRepairFlow.setOrderFlowId(orderFlow2.getOrderFlowId());
                            orderRepairFlowInsertList.add(newOrderRepairFlow);
//                            orderRepairFlowMapper.insertOrderRepairFlow(newOrderRepairFlow);


                        }
                    }



                }


            }

            RepairDetail repairDetail=new RepairDetail();
            repairDetail.setRepairId(oldOrderRepair.getRepairId());
            List<RepairDetail> repairDetailList = repairDetailMapper.selectAll(repairDetail);
            for (RepairDetail repairDetail1 : repairDetailList) {
                OrderRepairDete orderRepairDete1=new OrderRepairDete();
                orderRepairDete1.setRepairDetailId(repairDetail1.getRepairDetailId());
                orderRepairDete1.setOrderRepairId(oldOrderRepair.getOrderRepairId());
                List<OrderRepairDete> orderRepairDeteList = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete1);


                for (OrderRepairDete oldOrderRepairDete : orderRepairDeteList) {

                    OrderRepairDete newOrderRepairDete = new OrderRepairDete();
                    BeanUtils.copyProperties(oldOrderRepairDete,newOrderRepairDete);
                    newOrderRepairDete.setOrderRepairDeteId(null);
                    newOrderRepairDete.setOrderRepairId(newOrderRepairId);
                    orderRepairDeteInsertList.add(newOrderRepairDete);
//                    orderRepairDeteMapper.insertOrderRepairDete(newOrderRepairDete);

                }

            }


        }


        if(orderStationInsertList.size()!=0){
//            for (OrderStation list:orderStationInsertList  ) {
//                orderStationMapper.insertOrderStation(list);
//            }
            orderTechMapper.orderStationInsertList(orderStationInsertList);
        }
        if(orderDeteInsertList.size()!=0){
//            for (OrderDete list:orderDeteInsertList  ) {
//                orderDeteMapper.insertOrderDete(list);
//            }
            orderTechMapper.orderDeteInsertList(orderDeteInsertList);
        }
        if(orderBomInsertList.size()!=0){
//            for (OrderBom list:orderBomInsertList  ) {
//                orderBomMapper.insertOrderBom(list);
//            }
            orderTechMapper.orderBomInsertList(orderBomInsertList);
        }
        if(orderBadphenoInsertList.size()!=0){
//            for (OrderBadpheno list:orderBadphenoInsertList  ) {
//                orderBadphenoMapper.insertOrderBadpheno(list);
//            }
            orderTechMapper.orderBadphenoInsertList(orderBadphenoInsertList);
        }
        if(orderRepairFlowInsertList.size()!=0){
//            for (OrderRepairFlow list:orderRepairFlowInsertList  ) {
//                orderRepairFlowMapper.insertOrderRepairFlow(list);
//            }
            orderTechMapper.orderRepairFlowInsertList(orderRepairFlowInsertList);
        }
        if(orderRepairDeteInsertList.size()!=0){
//            for (OrderRepairDete list:orderRepairDeteInsertList  ) {
//                orderRepairDeteMapper.insertOrderRepairDete(list);
//            }
            orderTechMapper.orderRepairDeteInsertList(orderRepairDeteInsertList);
        }




        return  newOrderTechId;
        //复制流程完成
    }

    /**
     * 保存维修流程O
     */
    @Override
    public JSONObject saveOrderRepair(Order order) {
        try {
            //判空
            if (order == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (order.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=order.getBatchId();
            if (order.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=order.getOrderTechId();
            //获取版本
//            Integer orderId = order.getOrderId();
            //获取list集合
            List<OrderRepair> orderRepairList = new ArrayList<>();
            orderRepairList = order.getOrderRepairList();
            for (OrderRepair orderRepair : orderRepairList) {
                orderRepair.setOrderTechId(oldOrderTechId);
            }


            //获取数据库数据
            OrderRepair orderRepair1 = new OrderRepair();
            orderRepair1.setOrderTechId(oldOrderTechId);

            List<OrderRepair> oldorderRepairList = orderRepairMapper.queryOrderRepair(orderRepair1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldorderRepairList != null) {
                for (OrderRepair orderRepair : oldorderRepairList
                ) {
                    oldIds.add(orderRepair.getOrderRepairId());
                }
            }
            if (orderRepairList != null) {
                for (OrderRepair orderRepair : orderRepairList
                ) {
                    newIds.add(orderRepair.getOrderRepairId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = orderRepairMapper.deleteOrderRepair(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (orderRepairList != null) {
                for (OrderRepair orderRepair : orderRepairList
                ) {
                    if (orderRepair.getDataState() == null) {
                        orderRepair.setDataState(1);
                    }


                    if (orderRepair.getOrderRepairId() != null) {
                        if (orderRepair.getOrderRepairId().equals(0)) {
                            orderRepair.setOrderRepairId(null);
                            insertrow = insertrow + orderRepairMapper.insertOrderRepair(orderRepair);


                        } else {
                            updaterow = updaterow + orderRepairMapper.updateOrderRepair(orderRepair);
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, order,deleterow+insertrow+updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject queryOrderList(Order order) {
        Integer pagesize = order.getPagesize();
        Integer pageindex = order.getPageindex();
        if(pagesize != null && pageindex != null) {
            order.setIncept(pagesize*(pageindex - 1));
        }
        List<Order> orders = orderMapper.queryOrderByParam(order);
        if(!orders.isEmpty()){
            int count = orderMapper.countQueryOrderByParam(order);
            return JSONObject.oK(orders,count);
        }
        return JSONObject.oK(orders,null);
    }
    @Transactional(rollbackFor = Exception.class)
    public void orderLeadOrderFlow(Integer[] orderIds){
            List<Order> orderList = orderMapper.queryOrderListByOrdeId(orderIds);
            if (!orderList.isEmpty()) {
                //将对应版本新增到order_tech表中
                orderTechMapper.insertOrderTechList(orderList);
                //更新order表中的orderTechId
                orderMapper.updateOrderTech(orderList);
               //批量查询对应产品流程指定产品版本下的工序流程
                List<MatFlow> matFlowList = matFlowMapper.queryMatFlowList(orderList);
                //新增用的工序流程信息(存在重复的matFlowId,不同生产任务单存在相同的产品流程)
                List<MatFlow> insertMatFlowList=new ArrayList<>();

                if (!matFlowList.isEmpty()) {
                    for (Order order : orderList) {
                        for (MatFlow matFlow : matFlowList) {
                            if (order.getMatVerId().equals(matFlow.getMatVerId())) {
                                MatFlow targetMatFlow=new MatFlow();
                                BeanUtils.copyProperties(matFlow,targetMatFlow);
                                targetMatFlow.setOrderTechId(order.getOrderTechId());
                                insertMatFlowList.add(targetMatFlow);
                            }
                        }
                    }
                    //批量新增任务单流程下的工序流程
                    if( !insertMatFlowList.isEmpty() ){
                        orderFlowMapper.insertOrderFlowList(insertMatFlowList);


                        //批量查询对应的产品版本下的工序流程--工位    通过matFlowId查
                        List<MatStation> matStationList = matStationMapper.queryMatStationByMatFlowList(matFlowList);
                        List<MatStation> insertMatStationList=new ArrayList<>();
                        if (!matStationList.isEmpty()) {

                            for (MatFlow matFlow : insertMatFlowList) {
                                for (MatStation matStation : matStationList) {
                                    if (matFlow.getMatFlowId().equals(matStation.getMatFlowId())) {
                                        MatStation targetMatStation=new MatStation();
                                        BeanUtils.copyProperties(matStation,targetMatStation);
                                        targetMatStation.setOrderFlowId(matFlow.getOrderFlowId());
                                        insertMatStationList.add(targetMatStation);
                                    }
                                }
                            }
                            //批量新增对应的产品版本下的工序流程--工位
                            if(!insertMatStationList.isEmpty()){
                                orderStationMapper.insertOrderStationByMatStationList(insertMatStationList);
                            }

                        }
                        //批量查询对应的产品版本下的工序流程--采集项目
                        List<MatDete> matDeteList = matDeteMapper.queryMatDeteByMatFlowList(matFlowList);
                        List<MatDete> insertMatDeteList=new ArrayList<>();
                        if (!matDeteList.isEmpty()) {

                            for (MatFlow matFlow : insertMatFlowList) {
                                for (MatDete matDete : matDeteList) {
                                    if(matFlow.getMatFlowId().equals(matDete.getMatFlowId())){
                                        MatDete targetMatDete=new MatDete();
                                        BeanUtils.copyProperties(matDete,targetMatDete);
                                        targetMatDete.setOrderFlowId(matFlow.getOrderFlowId());
                                        insertMatDeteList.add(targetMatDete);
                                    }
                                }
                            }
                            //批量新增对应的产品版本下的工序流程--采集项目
                            if(!insertMatDeteList.isEmpty()){
                                orderDeteMapper.insertOrderDeteByMatDeteList(insertMatDeteList);
                            }

                        }
                        //批量查询对应的产品版本下的工序流程--关键物料
                        List<MatBom> matBomList = matBomMapper.queryMatBomByMatFlowList(matFlowList);
                        List<MatBom> insertMatBomList = new ArrayList<>();
                        if (!matBomList.isEmpty()) {
                            for (MatFlow matFlow : insertMatFlowList) {
                                for (MatBom matBom : matBomList) {
                                    if(matFlow.getMatFlowId().equals(matBom.getMatFlowId())){
                                        MatBom targetMatBom=new MatBom();
                                        BeanUtils.copyProperties(matBom,targetMatBom);
                                        targetMatBom.setOrderFlowId(matFlow.getOrderFlowId());
                                        insertMatBomList.add(targetMatBom);
                                    }
                                }
                            }
                            //批量新增对应的产品版本下的工序流程--关键物料
                            if( !insertMatBomList.isEmpty() ){
                                orderBomMapper.insertOrderBomByMatFlowList(insertMatBomList);
                            }

                        }
                        //批量查询对应的产品版本下的工序流程--不良现象
                        List<MatBadpheno> matBadphenoList = matBadphenoMapper.queryMatBadphenoByMatFlowList(matFlowList);
                        List<MatBadpheno> insertMatBadphenoList = new ArrayList<>();
                        if(!matBadphenoList.isEmpty()){

                            for (MatFlow matFlow : insertMatFlowList) {
                                for (MatBadpheno matBadpheno : matBadphenoList) {
                                    if(matFlow.getMatFlowId().equals(matBadpheno.getMatFlowId())){
                                        MatBadpheno targetMatBadpheno=new MatBadpheno();
                                        BeanUtils.copyProperties(matBadpheno,targetMatBadpheno);
                                        targetMatBadpheno.setOrderFlowId(matFlow.getOrderFlowId());
                                        insertMatBadphenoList.add(targetMatBadpheno);
                                    }
                                }
                            }
                            //批量新增对应的产品版本下的工序流程--不良现象
                            if ( !insertMatBadphenoList.isEmpty() ) {
                                orderBadphenoMapper.insertOrderBadphenoByMatBadphenoList(insertMatBadphenoList);
                            }

                        }
                    }
                }



                //批量查询对应的产品版本下的维修流程
                List<MatRepair> matRepairList = matRepairMapper.queryMatRepairByMatFlowList(orderList);
                //新增用的维修流程信息(存在重复的matRepairId,不同生产任务单存在相同的产品流程))
                List<MatRepair> insertMatRepairList = new ArrayList<>();
                if (!matRepairList.isEmpty()) {

                    for (Order order : orderList) {
                        for (MatRepair matRepair : matRepairList) {
                            if (order.getMatVerId().equals(matRepair.getMatVerId())) {
                                MatRepair targetMatRepair=new MatRepair();
                                BeanUtils.copyProperties(matRepair,targetMatRepair);
                                targetMatRepair.setOrderTechId(order.getOrderTechId());
                                insertMatRepairList.add(targetMatRepair);
                            }
                        }
                    }

                    if (!insertMatRepairList.isEmpty()) {
                        //批量新增任务单流程下的维修流程
                        orderRepairMapper.insertOrderRepairByMatRepairList(insertMatRepairList);

                        //批量查询产品流程指定版本下的维修流程--工序
                        List<MatRepairFlow> matRepairFlowList = matRepairFlowMapper.queryMatRepairFlowByMatRepairList(matRepairList);
                        List<MatRepairFlow> insertMatRepairFlowList = new ArrayList<>();
                        //这里新增需要 先获取到产品流程指定版本下的维修流程下的工序 此工序对应的工序流程id来找到任务单流程对应的工序流程id
                        if (!matRepairFlowList.isEmpty()) {

                            //新增的维修流程个数
                            for (MatRepair matRepair : insertMatRepairList) {
                                for (MatRepairFlow matRepairFlow : matRepairFlowList) {
                                    if (matRepairFlow.getMatRepairId().equals(matRepair.getMatRepairId())) {
                                        matRepairFlow.setOrderRepairId(matRepair.getOrderRepairId());
                                    }
                                    for (MatFlow matFlow : insertMatFlowList) {
                                        if (matRepairFlow.getMatFlowId().equals(matFlow.getMatFlowId())) {
                                            matRepairFlow.setOrderFlowId(matFlow.getOrderFlowId());
                                            MatRepairFlow targetMatRepairFlow=new MatRepairFlow();
                                            BeanUtils.copyProperties(matRepairFlow,targetMatRepairFlow);
                                            insertMatRepairFlowList.add(targetMatRepairFlow);
                                            break;
                                        }
                                    }

                                }

                            }
                            //批量新增对应的产品版本下的维修流程--工序
                            if ( !insertMatRepairFlowList.isEmpty() ) {
                                orderRepairFlowMapper.insertOrderRepairFlowByMatRepairFlow(insertMatRepairFlowList);
                            }

                        }
                        //批量查询对应的产品版本下的维修流程--维修采集项目
                        List<RepairDetail> repairDetailList = repairDetailMapper.queryRepairDetailByMatRepairList(insertMatRepairList);
                        List<RepairDetail> insertRepairDetailList = new ArrayList<>();
                        if (!repairDetailList.isEmpty()) {

                            for (MatRepair matRepair : insertMatRepairList) {
                                for (RepairDetail repairDetail : repairDetailList) {
                                    if (matRepair.getRepairId().equals(repairDetail.getRepairId())) {
                                        RepairDetail targerMatRepair=new RepairDetail();
                                        BeanUtils.copyProperties(repairDetail,targerMatRepair);
                                        targerMatRepair.setMatRepairId(matRepair.getMatRepairId());
                                        insertRepairDetailList.add(targerMatRepair);
                                    }
                                }
                            }
                            if ( !insertRepairDetailList.isEmpty()  ){
                                List<MatRepairDete> matRepairDeteList = matRepairDeteMapper.queryMatRepairDeteByRepairDetailList(insertRepairDetailList);
                                List<MatRepairDete> insertMatRepairDeteList = new ArrayList<>();
                                if (!matRepairDeteList.isEmpty()) {

                                    for (MatRepair matRepair : insertMatRepairList) {
                                        for (MatRepairDete matRepairDete : matRepairDeteList) {
                                            if (matRepairDete.getMatRepairId().equals(matRepair.getMatRepairId())) {
                                                MatRepairDete targetMatRepairDete=new MatRepairDete();
                                                BeanUtils.copyProperties(matRepairDete,targetMatRepairDete);
                                                targetMatRepairDete.setOrderRepairId(matRepair.getOrderRepairId());
                                                insertMatRepairDeteList.add(targetMatRepairDete);
                                            }
                                        }

                                    }
                                    //批量新增对应的产品版本下的维修流程--维修采集项目
                                    if ( !insertRepairDetailList.isEmpty() ){
                                        orderRepairDeteMapper.insertOrderRepairDeteByMatRepairDeteList(insertMatRepairDeteList);
                                    }

                                }
                            }



                        }
                    }


                }

            }
        }









}
